package com.hbl.huangbaolian1506101164.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hbl.huangbaolian1506101164.R;
import com.hbl.huangbaolian1506101164.model.Student;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {


    @InjectView(R.id.activity_main)
    LinearLayout activityMain;
    @InjectView(R.id.username)
    EditText username;
    @InjectView(R.id.password)
    EditText password;
    @InjectView(R.id.cb)
    CheckBox cb;
    @InjectView(R.id.clickLogin)
    Button clickLogin;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SharedPreferences sp = getSharedPreferences("shared",MODE_PRIVATE);
        editor = sp.edit();
        ButterKnife.inject(this);
    }

    @OnClick(R.id.clickLogin)
    public void onViewClicked() {

        if (cb.isChecked()) {
            editor.putString("name",username.getText().toString());
            editor.putString("pass",password.getText().toString());
            editor.commit();
            //                    ctrl+J:Toast全部补齐
            Toast.makeText(MainActivity.this, "checkBox已经选中,用户名和密码已经保存", Toast.LENGTH_SHORT).show();
        }else {
            //sp的清除，否则会保留上一个人的
            //1、全部数据的清除
//                        editor.clear();
//                        editor.commit();
            //2、部分数据的清除
            editor.remove("pass");
            editor.commit();
            Toast.makeText(MainActivity.this, "checkBox未被选中，用户名和密码未保存", Toast.LENGTH_SHORT).show();
        }


        final String name = username.getText().toString();
        final String pass = password.getText().toString();
        OkGo.get("http://www.mfcsjk.com/post?id=451")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
//                        textView.setText(s);
                        Gson gson = new Gson();
                        Student stu = gson.fromJson(s, Student.class);
                        String userN = stu.getName();
                        String userP = stu.getPassword();
                        if (userN.equals(name) && userP.equals(pass)) {
                            Intent intent = new Intent(MainActivity.this, Home.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(MainActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }

}
